import {
  imageUrl
} from "../../utils/imageutil"
Page({

  data: {
    imageUrl,
    GoodCategoryType: 1,
    list2_index: 0,
    company_type: '日常保洁',
    tap: [],
    list: [],
    page: 1,
    flag: true,
    title:'',
    cid: 2,
    type:130
  },
  onLoad: function (option) {
    if (option.company_type) {
      this.setData({
        company_type: option.company_type,
        title:option.company_type,
      })
    }
    wx.setNavigationBarTitle({
      title: option.company_type,
    })
    if (option.cid) {
      this.setData({
        cid: option.cid
      })
    }
    this.getTap()
  },
  openmap(e) {
    wx.navigateTo({
      url: '../housekeeping/index?store_id=' + e.currentTarget.dataset.store_id,
    })
  },
  getTap() {
    if(this.data.title == "幸福帮手"){
      getApp().globalData.post({
        url: '/api/applet.oyc/happy_type.html',
        data: {

        }
      }, true).then(res=>{
        this.setData({
          tap: res.data.data
        })
        this.data.tap.forEach((v, i) => {
          if (this.data.company_type == v.title) {
            this.setData({
              list2_index: i
            })
          }
        })
        this.getList()
      })
      return
    }
    getApp().globalData.post({
      data: {
        do: 'GoodCategory',
        cid: this.data.cid
      }
    }).then(res => {
      this.setData({
        tap: res.data.data
      })
      this.data.tap.forEach((v, i) => {
        if (this.data.company_type == v.title) {
          this.setData({
            list2_index: i
          })
        }
      })
      this.getList()
    })
  },
  onReachBottom() { //监听用户下拉
    if (this.data.flag) {
      this.getList()
    }
  },
  getList() {
    if (!this.data.flag) {
      return
    }
    this.setData({
      flag: false
    })
    if (this.data.title == "幸福帮手") {
      getApp().globalData.post({
        url: '/api/applet.oyc/happy_helper.html',
        data: {
          type: this.data.type
        }
      }, true).then(res => {
        wx.stopPullDownRefresh();
        if (res.data.data.length == 0) {
          this.setData({
            flag: false
          })
        } else {
          let arr = this.data.list.concat(res.data.data);
          this.setData({
            list: arr,
            page: this.data.page + 1,
            flag: true
          })
        }
      })
      return
    }
    getApp().globalData.posts('/api/app/work/sorting/ouyoung_api/dishesListNew', {
      company_type: this.data.company_type,
      page: this.data.page,
      pagesize: 10,
      url_type: 1,
    }).then(res => {
      wx.stopPullDownRefresh();
      if (res.data.data.length == 0) {
        this.setData({
          flag: false
        })
      } else {
        let arr = this.data.list.concat(res.data.data);
        this.setData({
          list: arr,
          page: this.data.page + 1,
          flag: true
        })
      }
    })
    return
    getApp().globalData.post({
      data: {
        do: 'DishesListNew',
        company_type: this.data.company_type,
        page: this.data.page,
        pagesize: 10,
        uid: wx.getStorageSync('userInfo').id
      }
    }).then(res => {
      wx.stopPullDownRefresh();
      if (res.data.data.length == 0) {
        this.setData({
          flag: false
        })
      } else {
        let arr = this.data.list.concat(res.data.data);
        this.setData({
          list: arr,
          page: this.data.page + 1,
          flag: true
        })
      }
    })
  },
  to_make(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: './make?good_id=' + id,
    })
  },
  changetype(e) { //顶部选项卡
    this.setData({
      list2_index: e.currentTarget.dataset.index,
      company_type: e.currentTarget.dataset.title
    })
    if(this.data.title == "幸福帮手"){
      this.setData({
        type:e.currentTarget.dataset.id
      })
    }
    this.setData({
      list: [],
      flag: true,
      page: 1
    })
    this.getList()
  },
})